// pages/myteams/myteams.js
const app = getApp() 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myteams:[]
  },
  lookDetail: function (e) {
    var index = e.currentTarget.dataset.teamindex
    app.globalData.team = this.data.myteams[index]
    wx.navigateTo({
      url: '../teamDetail/teamDetail',
    })
  },
  editTeam:function(e){
    //存储当前点击的组队信息到全局对象中，在显示页面中从全局对象中拿到点击的组队信息
    app.globalData.myteam = e.currentTarget.dataset.myteam
    //跳转到编辑页面
    wx.navigateTo({
      url: '../editTeam/editTeam',
    })
  },
  
  deleteTeam:function(e){
    var index = e.currentTarget.dataset.teamindex

    wx.request({
      url: 'http://' + app.globalData.ip +'/team/deleteTeamByTeamid',
      data: { userID: app.globalData.userInfo.openId,teamID:this.data.myteams[index].teamID},
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        if(res.data.msg==1){
          this.data.myteams.splice(index, 1)
          this.setData({
            myteams: this.data.myteams
          })
          wx.showToast({
            title: '删除成功',
          })
        }else{
          wx.showModal({
            content: '服务端删除失败',
          })
        }
        
      }

    })

    
  },
  getMyTeams:function(){
    wx.request({
      url: 'http://' + app.globalData.ip +'/team/getMyTeams',
      data: { userID: app.globalData.userInfo.openId},
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        var teams = res.data.myteams
        for (var i = 0; i < teams.length; ++i) {
          var d = new Date(teams[i]['createTime'])
          teams[i]['createTime'] = d.getFullYear() + '.' + d.getMonth() + '.' + d.getDate()
        } 
        this.setData({
          myteams:teams
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getMyTeams()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyTeams()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})